package hu.bme.mit.rifl.datamodel;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Random;

/**
 * The work item of the workflow.
 */
public class Order implements Comparable<Object>, Serializable {
	private final int orderID;
	private String state;
	private int[] stateCodes;
	private boolean isBooked;

	/**
	 * Default constructor. The orderID will be generated.
	 */
	public Order() {
		this.orderID = generateID();
		this.isBooked = false;
		this.stateCodes = new int[8];
	}

	/**
	 * Constructor for custom orderID to be assigned.
	 * 
	 * @param orderID
	 *            the ID of the work item
	 */
	public Order(int orderID) {
		this.orderID = orderID;
		this.isBooked = false;
		this.stateCodes = new int[8];
	}

	/**
	 * Generates a random ID for the work item.
	 * 
	 * @return random ID
	 */
	private int generateID() {
		Calendar calendar = Calendar.getInstance();
		Random generator = new Random(calendar.get(Calendar.MILLISECOND));
		return generator.nextInt();
	}

	/**
	 * Getter for the work item ID.
	 * 
	 * @return the work item ID
	 */
	public int getOrderID() {
		return orderID;
	}

	/**
	 * Getter for the state.
	 * 
	 * @return the state of the work item
	 */
	public String getState() {
		return this.state;
	}

	/**
	 * Setter for the state.
	 * 
	 * @param state
	 *            the new state of the work item
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * Getter for the state codes.
	 * 
	 * @return array of the state codes
	 */
	public int[] getStateCodes() {
		return this.stateCodes;
	}

	/**
	 * Setter for the state codes.
	 * 
	 * @param position
	 *            the position of the array storing the state codes
	 */
	public void setStateCodes(int position) {
		this.stateCodes[position] = 1;
	}

	/**
	 * Getter for the isBooked property.
	 * 
	 * @return whether the work item is booked or not
	 */
	public boolean isBooked() {
		return this.isBooked;
	}

	/**
	 * Setter for the isBooked property.
	 * 
	 * @param isBooked
	 *            whether the work item is booked or not
	 */
	public void setBooked(boolean isBooked) {
		this.isBooked = isBooked;
	}

	/**
	 * Inherited method from the Comparable interface. Returns priorities work
	 * item for the PriorityQueues containing them in a random way.
	 */
	@Override
	public int compareTo(Object anotherOrder) {
		Random random = new Random();
		if (random.nextBoolean()) {
			return 1;
		} else {
			return -1;
		}
	}
}